package com.gec.auction.service.impl;

import com.gec.auction.mapper.AuctionCustomerMapper;
import com.gec.auction.mapper.AuctionMapper;
import com.gec.auction.mapper.AuctionrecordMapper;
import com.gec.auction.pojo.Auction;
import com.gec.auction.pojo.AuctionCustomer;
import com.gec.auction.pojo.Auctionrecord;
import com.gec.auction.service.AuctionService;
import com.gec.auction.tools.AuctionCustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * @author Irene_baebae
 * @version 1.0
 */
@Service
public class AuctionServiceImpl implements AuctionService {
    @Autowired
    private AuctionMapper auctionMapper;

    @Autowired
    private AuctionCustomerMapper auctionCustomerMapper;

    @Autowired
    private AuctionrecordMapper auctionrecordMapper;

    public List<Auction> getAuction(Auction condition, String auctionname, String auctiondesc, BigDecimal auctionstartprice) {
        List<Auction> list = auctionMapper.findAll(auctionname,auctiondesc,auctionstartprice);
        return list;
    }

    @Override
    public Auction getAuctionById(int auctionId) {
        Auction auction = auctionMapper.selectByPrimaryKey(auctionId);
        return auction;
    }

    @Override
    public int addAuction(Auction auction) {
        return auctionMapper.insert(auction);
    }

    @Override
    public int deleteByPrimaryKey(Long auctionId) {
        auctionMapper.deleteByPrimaryKey(auctionId);
        return auctionMapper.deleteByPrimaryKey(auctionId);
    }



    /**
     *   1.有竞拍记录
     a.你出的价格不能低于所有价格的中的最高价
     给出错误提示: 不能低于所有价格的最高价  -> 异常
     b.你竞拍的时间不能晚于 当前活动的结束时间
     给出错误提示: 不能晚于活动的结束时间  -> 异常
     2.没有竞拍记录
     如果你出的价格低于 起拍价
     给出错误提示:  当前价格不能低于起拍价
     *
     */
    @Override
    public void addAuctionRecored(Auctionrecord record) throws AuctionCustomerException {
        Auction auction = auctionCustomerMapper.selectAuctionByAuctionId(record.getAuctionid());
        //判断竞拍时的时间是否晚于活动结束时间
        if(new Date().after(auction.getAuctionendtime())){
            throw new AuctionCustomerException("当前时间不能晚于活动结束时间！！！");
        }
        //判断当前拍卖商品是否存在竞拍记录
        if(auction.getAuctionList().size()>0){
            //存在竞拍记录时，获得最大竞拍价格的记录
            Auctionrecord maxRecored = auction.getAuctionList().get(0);
            //判断当前竞拍价格是否小于最高竞拍价格，如果小于就给出提示
            if(record.getAuctionprice().compareTo(maxRecored.getAuctionprice())<1){
                throw new AuctionCustomerException("当前价格不能小于所有记录中的最高价格！！！");
            }
        }else{ //没有任何竞拍记录
            //如果当前竞拍的价格小于了商品起拍价格时，给出提示信息
            if(record.getAuctionprice().compareTo(auction.getAuctionstartprice())<1){
                throw new AuctionCustomerException("当前价格不能小于起拍价！！！");
            }
        }
        //如果以上都没有进入，说明符合竞拍要求，就添加一条新竞拍记录到数据库中
        auctionrecordMapper.insert(record);
    }


    @Override
    public List<AuctionCustomer> selectAuctionEndTime() {
        return auctionCustomerMapper.selectAuctionEndTime();
    }


    @Override
    public List<Auction> selectAuctionNoEndTime() {
        return auctionCustomerMapper.selectAuctionNoEndTime();
    }

}
